package com.bucg.cjcloud.gb28181.media.conf;

import com.bucg.cjcloud.gb28181.media.model.CheckModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * HttpClient..
 *
 * @author Lizhong
 * @date 2020/7/23
 */
@Configuration
public class HttpClient {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    MediaServerConfiguration mediaServerConfiguration;
    @Autowired
    MediaProperties mediaProperties;


    public boolean checkSsrr(String ip , Integer streamId){
        String i = Integer.toHexString(streamId);
        Map<String,String> map = new HashMap<>();
        map.put("stream_id",i);
        map.put("secret","035c73f7-bb6b-4889-a715-d9eb2d1925cc");
        String host = mediaServerConfiguration.getHostIpHash(ip).getKey();

        CheckModel forObject = restTemplate.getForObject(host + mediaProperties.getCheckUrl(), CheckModel.class, map);
        if(forObject!=null && forObject.getIsExist()!=null &&forObject.getIsExist()) return true;
        return false;
    }

}
